using UnityEngine;
using System.Collections;

public class Note {
    public int index = 0;
    public float pitch;
    public float startTime;
    public float length;
    public int vol = 100;
    public int pan = 0;
    public string numMidiNote;

    public string text;


    public Note(Hashtable noteIn, Hashtable noteOut, Hashtable modifiers)
    {
        pitch = (float)noteIn["pitch"];
        startTime = (float)noteIn["time"];
        length = (float)noteOut["time"] - (float)noteIn["time"];
        numMidiNote = (string)noteIn["numNote"];
        text = textNotation(false);
        if (modifiers.ContainsKey("vol"))
            vol = (int)modifiers["vol"];
        if (modifiers.ContainsKey("pan"))
            pan = (int)modifiers["pan"];
    }

    public override string ToString()
    {
        return "note ( " + text + " (" + numMidiNote + ") ;\tpitch=" + pitch + " ;\tstart=" + startTime + " ;\tend=" + (startTime + length) + " ;\tlength=" + length + " ;\tvol=" + vol + " ;\tpan=" + pan + " )";
    }
    public override int GetHashCode()
    {
        return Mathf.RoundToInt((index + pitch + startTime + length) * 100);
    }
    public override bool Equals(object obj)
    {
        return ((Note)obj).GetHashCode() == GetHashCode();
    }

    public bool isOverlap(Note note)
    {
        if (!note.Equals(this))
        {
            bool startIn = note.startTime >= this.startTime && note.startTime < (this.startTime + this.length);
            bool endIn = (note.startTime + note.length) > this.startTime && (note.startTime + note.length) <= (this.startTime + this.length);
            if (this.pitch != note.pitch && (startIn || endIn))
                return true;
        }
        return false;
    }

    public bool isSharpNote()
    {
        int num = hexToInt(numMidiNote) % 12;
        if (num < 5)
            return num % 2 > 0;
        return num % 2 == 0;
    }

    public int positionInPartition()
    {
        int num = hexToInt(numMidiNote);
        int nbPrevRanges = num / 12;
        int inRangePosition = num % 12;
        if (inRangePosition >= 5)
            inRangePosition++;
        return 7 * nbPrevRanges + (inRangePosition / 2);
    }

    public string textNotation(bool french)
    {
        string[] notations;
        int offsetRange;
        if (!french)
        {
            notations = new string[12] { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
            offsetRange = -2;
        }
        else
        {
            notations = new string[12] { "do", "do#", "re", "re#", "mi", "fa", "fa#", "sol", "sol#", "la", "la#", "si" };
            offsetRange = -3;
        }
        int num = hexToInt(numMidiNote);
        int gamme = num / 12 + offsetRange;
        int pos = num % 12;
        return notations[pos] + gamme;
    }

    private int hexToInt(string hex)
    {
        return int.Parse(hex.Replace("h", ""), System.Globalization.NumberStyles.HexNumber);
    }
}
